package config

import (
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"log"
)

var DeviceConfig *Config

// Config 配置结构体，与YAML配置对应
type Config struct {
	Server struct {
		ListenAddr string `yaml:"listen_addr"`
	} `yaml:"server"`
	Http struct {
		ListenAddr string `yaml:"listen_addr"`
	} `yaml:"http"`
	Mysql struct {
		Dsn string `yaml:"dsn"`
	} `yaml:"mysql"`
}

// LoadConfig 从文件加载配置
func LoadConfig(filename string) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatalf("load config file failed, err: %v", err)
		return
	}

	var config Config
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		log.Fatalf("unmarshal config file failed, err: %v", err)
		return
	}

	DeviceConfig = &config
	//日志
	log.Printf("config load success, server: %v, http: %v, mysql: %v", config.Server, config.Http, config.Mysql)
}
